package com.longrise.android.byjk.plugins.tabfirst.homeAd;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.app.FragmentManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.target.Target;
import com.longrise.LEAP.Base.Objects.EntityBean;


import java.io.File;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by callxkj on 2018/3/9.
 * 负责首页广告的弹窗
 */

public class HomeAdHelper {

    private static final String TAG = "HomeAdHelper";
    private Map<String, String> mDrawableMap = new HashMap<>();
    private final EntityBean[] mImageUrls;
    private boolean mIsfirst = true;//只能显示一次
    private boolean mIsTabFirst = true; //刚开始进app的时候默认的是第一个tab

    public HomeAdHelper(EntityBean[] imageUrls) {
        this.mImageUrls = imageUrls;
    }

    /**
     * 显示广告
     */
    public void toShowAd(Context context, FragmentManager fragmentManager) {
        getImage(context, fragmentManager);
    }

    /**
     * tab切换
     *
     * @param isTabFirst 当前是否是第一个tab
     */
    public void checkeAdShowed(boolean isTabFirst, Context context, FragmentManager fragmentManager) {
        this.mIsTabFirst = isTabFirst;
        if (mIsfirst && mIsTabFirst) {
            getImage(context, fragmentManager);
        }
    }

    private void getImage(Context context, FragmentManager fragmentManager) {
        if (mImageUrls == null || mImageUrls.length == 0) {//如果图片url则不弹窗
            return;
        }
        mDrawableMap.clear();
        try {
            for (int x = 0; x < mImageUrls.length; x++) {
                EntityBean imageUrlBean = mImageUrls[x];
                loadImage(context, imageUrlBean, fragmentManager);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void loadImage(final Context context, EntityBean bean, final FragmentManager fragmentManager) {
        final String picpath = bean.getString("picpath");
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                FutureTarget<File> target = Glide.with(context)
                        .load(picpath)
                        .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
                final File imageFile = target.get();
                String absolutePath = imageFile.getAbsolutePath();
                e.onNext(absolutePath);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        toShowDialog(picpath, s, fragmentManager);

                    }
                });

    }

    /**
     * 图片全部下载完了后 显示弹框
     */
    private void toShowDialog(String url, String localPath, FragmentManager fragmentManager) {
        mDrawableMap.put(url, localPath);
        if (mImageUrls != null && mDrawableMap.size() == mImageUrls.length) {
            Parcelable[] array = new Parcelable[mImageUrls.length];
            for (int x = 0; x < mImageUrls.length; x++) {
                EntityBean imageUrlBean = mImageUrls[x];
                String linkedtype = imageUrlBean.getString("linkedtype");
                String urltype = imageUrlBean.getString("urltype");
                String linkedurl = imageUrlBean.getString("linkedurl");
                String picpath = imageUrlBean.getString("picpath");
                String title = imageUrlBean.getString("title");
                String locpath = mDrawableMap.get(picpath);

                AdDialogBean dialogBean = new AdDialogBean();
                dialogBean.setLinkedtype(linkedtype);
                dialogBean.setUrltype(urltype);
                dialogBean.setLinkedurl(linkedurl);
                dialogBean.setPicpath(picpath);
                dialogBean.setTitle(title);
                dialogBean.setLocalpath(locpath);
                array[x] = dialogBean;
            }
            HomeAdDialogFragment dialogFragment = HomeAdDialogFragment.newInstance(array);
            if (mIsfirst && mIsTabFirst) {//当没显示过的时候 并且 选择的是第一个tab的时候才显示
                dialogFragment.show(fragmentManager, "homeADdialogFragment");
                mIsfirst = false;
            }
        }
    }
}
